import { RectNode, CircleNode, DiamondNode } from '@logicflow/core'
import { NODE_TYPES, NODE_CONFIGS } from './NodeTypes'

// 开始节点
class StartNode extends CircleNode {
  static extendKey = 'StartNode'
  setAttributes() {
    this.r = 25
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    const { fill, stroke } = NODE_CONFIGS[NODE_TYPES.START]
    return {
      ...style,
      fill,
      stroke
    }
  }

  getDefaultAnchor() {
    return []
  }

  getConnectedSourceRules() {
    return {
      message: '开始节点不能作为连线的起点',
      validate: () => false
    }
  }
}

// 结束节点
class EndNode extends CircleNode {
  static extendKey = 'EndNode'
  setAttributes() {
    this.r = 25
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    const { fill, stroke } = NODE_CONFIGS[NODE_TYPES.END]
    return {
      ...style,
      fill,
      stroke
    }
  }

  getDefaultAnchor() {
    return []
  }

  getConnectedTargetRules() {
    return {
      message: '结束节点不能作为连线的终点',
      validate: () => false
    }
  }
}

// Web操作节点
class WebActionNode extends RectNode {
  setAttributes() {
    this.width = 120
    this.height = 60
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    style.fill = '#95de64'
    style.stroke = '#73d13d'
    return style
  }

  getDefaultAnchor() {
    return [
      {
        x: 0.5,
        y: 0,
        id: 'top'
      },
      {
        x: 1,
        y: 0.5,
        id: 'right'
      },
      {
        x: 0.5,
        y: 1,
        id: 'bottom'
      },
      {
        x: 0,
        y: 0.5,
        id: 'left'
      }
    ]
  }
}

// 桌面操作节点
class DesktopActionNode extends RectNode {
  setAttributes() {
    this.width = 120
    this.height = 60
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    style.fill = '#69c0ff'
    style.stroke = '#40a9ff'
    return style
  }

  getDefaultAnchor() {
    return [
      {
        x: 0.5,
        y: 0,
        id: 'top'
      },
      {
        x: 1,
        y: 0.5,
        id: 'right'
      },
      {
        x: 0.5,
        y: 1,
        id: 'bottom'
      },
      {
        x: 0,
        y: 0.5,
        id: 'left'
      }
    ]
  }
}

// AI决策节点
class AIDecisionNode extends DiamondNode {
  setAttributes() {
    this.rx = 50
    this.ry = 35
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    style.fill = '#b37feb'
    style.stroke = '#9254de'
    return style
  }

  getDefaultAnchor() {
    return [
      {
        x: 0.5,
        y: 0,
        id: 'top'
      },
      {
        x: 1,
        y: 0.5,
        id: 'right'
      },
      {
        x: 0.5,
        y: 1,
        id: 'bottom'
      },
      {
        x: 0,
        y: 0.5,
        id: 'left'
      }
    ]
  }
}

// 条件判断节点
class ConditionNode extends DiamondNode {
  static extendKey = 'ConditionNode'
  setAttributes() {
    this.rx = 50
    this.ry = 35
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    const { fill, stroke } = NODE_CONFIGS[NODE_TYPES.CONDITION]
    return {
      ...style,
      fill,
      stroke
    }
  }

  getDefaultAnchor() {
    return [
      {
        x: 0.5,
        y: 0,
        id: 'top'
      },
      {
        x: 1,
        y: 0.5,
        id: 'right'
      },
      {
        x: 0.5,
        y: 1,
        id: 'bottom'
      },
      {
        x: 0,
        y: 0.5,
        id: 'left'
      }
    ]
  }
}

// 动作节点
class ActionNode extends RectNode {
  static extendKey = 'ActionNode'
  setAttributes() {
    this.width = 120
    this.height = 60
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    const { fill, stroke } = NODE_CONFIGS[NODE_TYPES.ACTION]
    return {
      ...style,
      fill,
      stroke
    }
  }

  getDefaultAnchor() {
    return [
      {
        x: 0.5,
        y: 0,
        id: 'top'
      },
      {
        x: 1,
        y: 0.5,
        id: 'right'
      },
      {
        x: 0.5,
        y: 1,
        id: 'bottom'
      },
      {
        x: 0,
        y: 0.5,
        id: 'left'
      }
    ]
  }
}

// 延迟节点
class DelayNode extends RectNode {
  static extendKey = 'DelayNode'
  setAttributes() {
    this.width = 120
    this.height = 60
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    const { fill, stroke } = NODE_CONFIGS[NODE_TYPES.DELAY]
    return {
      ...style,
      fill,
      stroke
    }
  }

  getDefaultAnchor() {
    return [
      {
        x: 0.5,
        y: 0,
        id: 'top'
      },
      {
        x: 1,
        y: 0.5,
        id: 'right'
      },
      {
        x: 0.5,
        y: 1,
        id: 'bottom'
      },
      {
        x: 0,
        y: 0.5,
        id: 'left'
      }
    ]
  }
}

// 子流程节点
class SubprocessNode extends RectNode {
  static extendKey = 'SubprocessNode'
  setAttributes() {
    this.width = 120
    this.height = 60
  }

  getNodeStyle() {
    const style = super.getNodeStyle()
    const { fill, stroke } = NODE_CONFIGS[NODE_TYPES.SUBPROCESS]
    return {
      ...style,
      fill,
      stroke
    }
  }

  getDefaultAnchor() {
    return [
      {
        x: 0.5,
        y: 0,
        id: 'top'
      },
      {
        x: 1,
        y: 0.5,
        id: 'right'
      },
      {
        x: 0.5,
        y: 1,
        id: 'bottom'
      },
      {
        x: 0,
        y: 0.5,
        id: 'left'
      }
    ]
  }
}

// 注册自定义节点
export function registerNodes(lf) {
  // 开始节点（圆形）
  lf.register(NODE_TYPES.START, {
    view: CircleNode,
    model: CircleNode,
    ...NODE_CONFIGS[NODE_TYPES.START]
  })

  // 结束节点（圆形）
  lf.register(NODE_TYPES.END, {
    view: CircleNode,
    model: CircleNode,
    ...NODE_CONFIGS[NODE_TYPES.END]
  })

  // 任务节点（矩形）
  lf.register(NODE_TYPES.TASK, {
    view: RectNode,
    model: RectNode,
    ...NODE_CONFIGS[NODE_TYPES.TASK]
  })

  // 条件节点（菱形）
  lf.register(NODE_TYPES.CONDITION, {
    view: DiamondNode,
    model: DiamondNode,
    ...NODE_CONFIGS[NODE_TYPES.CONDITION]
  })

  // 并行节点（矩形）
  lf.register(NODE_TYPES.PARALLEL, {
    view: RectNode,
    model: RectNode,
    ...NODE_CONFIGS[NODE_TYPES.PARALLEL]
  })

  // 顺序节点（矩形）
  lf.register(NODE_TYPES.SEQUENCE, {
    view: RectNode,
    model: RectNode,
    ...NODE_CONFIGS[NODE_TYPES.SEQUENCE]
  })
} 